package com.sunmouse.toolkit.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuItem;

import com.sunmouse.toollib.utils.LogUtils;
import com.sunmouse.toollib.utils.system.PermissionUtil;
import com.sunmouse.toollib.widget.FootNavLayout;
import com.sunmouse.toollib.widget.OnTabChangedListener;
import com.sunmouse.toolkit.BaseApp;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.constant.Config;
import com.sunmouse.toolkit.constant.Value;
import com.sunmouse.toolkit.util.PageUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends ToolbarActivity {


    @BindView(R.id.m_tab)
    FootNavLayout mTab;

    public static final int REQUEST_CODE = 5;
    private Fragment[] fragments;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

    @Override
    public void initNav() {
        setTitle("main");
        getToolbar().setNavigationIcon(null);
    }

    @Override
    public void init() {
        super.init();
        initView();

        String[] recordPermission = {
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA
        };
        if (!PermissionUtil.isPermitted(getActivity(), recordPermission)) {
            PermissionUtil.requestPermission(getActivity(), recordPermission, REQUEST_CODE);
        }


        LogUtils.i("TAG", "----" + getExternalFilesDir(null));

//        String src="fdjfsmflsk\nfndsfjojf\nvskcsnd  sdnk\n";
//
//        String s=src.trim();
//        LogUtils.v("-------" + StringUtils.isValidNo("15527825713"));
//        String s="";
//        LogUtils.v("-------" +s.length() );
//        ThreadPoolUtils.execute(new Runnable() {
//            @Override
//            public void run() {
//                com.kvin.toolkit.utils.http.HttpUtil.init()
//                        .url("https://www.baidu.com")
//                        .flag("baidu")
//                        .build(getActivity())
//                        .get(null);
//            }
//        });
//        JSONObject obj = new JSONObject();
//        HashMap<String, String> ps = new HashMap<>();
//        ps.put("p-1", "v-1");
//        ps.put("p-2", "v-2");
//        ps.put("p-3", "v-3");
//        JSONObject pp = new JSONObject(ps);
//        JSONArray array = new JSONArray();
//        array.put(pp);
//        array.put(pp);
//        array.put(pp);
//        array.put(pp);
//        LogUtils.v("TAG", array.toString());
//
//        try {
//            obj.put("list", array);
//            LogUtils.v("TAG", obj.toString());
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        int i=0;
//        while (i<10){
//            if (i==8)break;
//            LogUtils.v("------i"+i++);
//
//        }
//
//        LogUtils.v("-----stop----");

//        ArrayList<ClientBean> d1= new ArrayList<>();
//        for (int i = 0; i < 5; i++) {
//            d1.add(new ClientBean("client-" + i, null));
//        }
//
//        ArrayList<ClientBean> d2=new ArrayList<>();
//        d2.addAll(d1);
//
//        ClientBean bean=d1.get(4);
//        bean.setName("I come back again");
//
//        LogUtils.v("---bean="+bean.getName()+":d1="+d1.get(4).getName()+":d2="+d2.get(4).getName());
    }

    /**
     * init view
     */
    private void initView() {
        fragments = new Fragment[]{
                PageUtil.getFragment(PageUtil.Index.HOME),
                PageUtil.getFragment(PageUtil.Index.MEDIA),
                PageUtil.getFragment(PageUtil.Index.MORE),
                PageUtil.getFragment(PageUtil.Index.SETTING)
        };
        int[] icons = {R.drawable.tab_home_selector,
                R.drawable.tab_media_selector,
                R.drawable.tab_more_selector,
                R.drawable.tab_setting_selector};

        String[] tabNames = {"home", "media", "more", "setting"};
        this.mTab.init(this, fragments, icons, tabNames, 0);
        this.mTab.setOnTabChangedListener(mTabChangedListener);
    }


    /**
     * on table changed listener
     */
    private OnTabChangedListener mTabChangedListener = new OnTabChangedListener() {
        @Override
        public void onTabChanged(int position) {
            switch (position) {
                case 0:
                    setTitle("main");
                    break;
                case 1:
                    setTitle("media");
                    break;
                case 2:
                    setTitle("more");
                    break;
                case 3:
                    setTitle("setting");
                    break;
            }
        }
    };


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                return;
            }
            BaseApp.getInstance().exit();

        }
    }

    /**
     * set menu
     *
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * handle menu events
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        for (int i = 0; i < fragments.length; i++) {
            fragments[i].onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mIntent = new Intent(Value.Action.EXCEPTION);
        mIntent.putExtra(Config.Key.CONTROL, Value.Control.STOP);
        sendBroadcast(mIntent);
    }
}
